package com.zxc.controller.service;

import com.zxc.entity.SctService;
import com.zxc.service.SctServiceService;
import com.zxc.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/distribution")
public class DistributionController {

    @Autowired
    private SctServiceService sctServiceService;

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping({"/{skipCount}/{pageSize}/{searchName}","/{skipCount}/{pageSize}"})
    @ResponseBody
    public Map<String,Object> getSctServiceListOrByCustName(@PathVariable Integer skipCount,
                                                          @PathVariable Integer pageSize,
                                                          @PathVariable(required = false) String searchName){
        Map<String,Object> modelMap = new HashMap<>();
        int skipCounts = (skipCount-1)*pageSize;
        String svrType = "新创建";
        List<SctService> sctServiceList  = sctServiceService.getSctServiceListBySvrStatus(skipCounts,pageSize,svrType);
        modelMap.put("dataList",sctServiceList);
        int total = sctServiceService.selectSctServiceCountByStatus(svrType);
        modelMap.put("total",total);
        return modelMap;
    }


    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> delete(Integer[] svrId){
        Map<String,Object> modelMap = new HashMap<>();
        int result = sctServiceService.delete(svrId);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }

    @RequestMapping("/updateDistribution")
    @ResponseBody
    public Map<String,Object> updateDistribution(Long svrId,Long svrDueId){
        Map<String,Object> modelMap = new HashMap<>();
        SctService sctService = new SctService();
        sctService.setSvrId(svrId);
        sctService.setSvrDueId(svrDueId);
        String svrDueTo = sysUserService.selectSysUserBySysUserId(String.valueOf(svrDueId));
        sctService.setSvrDueTo(svrDueTo);
        sctService.setSvrStatus("已分配");
        sctService.setSvrDueDate(new Date());
        int result = sctServiceService.updateDistributionById(sctService);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }
}